home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c-part1 / 1100 < prev    next >
Encoding:
Internet Message Format  |  1996-08-05  |  3.5 KB

  1. Path: news.lpr.carel.fi!usenet
  2. From: aril@cmt.lpr.mail.carel.fi (Ari Lukumies)
  3. Newsgroups: comp.lang.c
  4. Subject: Re: QUESTION about pointers and structures
  5. Date: Thu, 11 Jan 1996 12:58:25 GMT
  6. Organization: Carelcomp Forest Oy
  7. Message-ID: <4d31u1$kdv@tahko.lpr.carel.fi>
  8. References: <4d1rqg$bse@mirv.unsw.edu.au>
  9. NNTP-Posting-Host: renoir.cclahti.carel.fi
  10. X-Newsreader: Forte Free Agent 1.0.82
  11.  
  12. gwong@cse.unsw.edu.au (Geoffrey  Wong) wrote:
  13.  
  14. >Hi guys,
  15. >    I am wondering if anyone of you c legends could help me. I have a
  16. >    rather complex data structure.
  17. >    It is a pointer to structure which contains pointer to structures.
  18. >    I don't know if I need to allocate space for variables of this complex
  19. >    type before I can do the assignments.
  20. >    
  21. >    I have a variable called background.
  22. >    declared as:-
  23. >    SLiteral        *background; 
  24. >    
  25. >    Now SLiteral is:-
  26. >    
  27. >    typedef struct _slit_rec        *SLiteral,      /* Used in rlgg */
  28. >    
  29. >    and _slit_rec is:-
  30. >    
  31. >    struct _slit_rec
  32. >{
  33. >    char            Sign;           /* 0=negated, 1=pos, 2=determinate */
  34. >    Relation        Rel;
  35. >    Const           *Constants; /* During an rlgg process Constant and Var is 
  36. >                    coded using the same data type. This is 
  37. >                    possible since Const is larger than Var! */
  38. >    Which           *ConstVar;      /* 0=const, 1=Var  for each Constants */
  39. >    int             WeakLits;       /* value up to this literal */
  40. >    Ordering        *ArgOrders,     /* recursive lits: =, <, >, # */
  41. >            *SaveArgOrders; /* copy during pruning */
  42. >    float           Bits;           /* encoding length */
  43. >};
  44. >Now my question is can I just say background[i]->Sign = ???? or do I need to
  45. >allocate space for background[i] or do I need to allocate space for *background
  46. >Any help would be appreciated, I am rather new to c, don't know much tricks
  47. >yet.
  48.  
  49. >geoff.
  50.  
  51. I take it you want to have a variable background, which is a pointer
  52. to many struct _slit_rec's? If so, you will do either
  53.  
  54.     typedef struct _slit_rec    *SLiteral;
  55.     SLiteral            background;
  56.  
  57. or
  58.     typedef struct _slit_rec    SLiteral;
  59.     SLiteral            *background;
  60.  
  61. I'd prefer the latter, because the declaration of background clearly
  62. shows that it's to be a pointer, so in the examples below I've taken
  63. that approach.
  64.  
  65. In your code, you'll end up actually doing
  66.  
  67.     struct _slit_rec        **background;
  68.  
  69. because SLiteral is already a pointer type (hence the *), and you
  70. define background as a pointer of this (pointer) type. If you want to
  71. have, say, 100 background objects, you can allocate them all at once:
  72.  
  73.     background = (SLiteral *)calloc(100, sizeof(SLiteral));
  74.  
  75. and then you can use
  76.  
  77.     for (i = 0; i < 100; i++)
  78.         something = background[i].Sign;
  79.  
  80. Remember to free(background) when you're done.
  81.  
  82. If you still (for some unknown reason) wanted to use your original
  83. declaration (**background), you would have to (Note: here SLiteral is
  84. a pointer type to struct _slit_rec):
  85.  
  86.     background = (SLiteral*)calloc(100, sizeof(SLiteral));
  87.     for (i = 0; i < 100; i++)
  88.         background[i] = (SLiteral)calloc(1, sizeof(struct _slit_rec));
  89.  
  90. You must use sizeof(struct _slit_rec) here, because the
  91. sizeof(SLiteral) is only the size of the pointer (usually 4 bytes).
  92.  
  93. As myself, I usually use structure definitions such as:
  94.  
  95.     typedef struct a_struct {
  96.         .....
  97.     } A_STRUCT, *PA_STRUCT;
  98.  
  99. which will give me a) typedef for the structure itself (A_STRUCT) and
  100. a typedef for a pointer to is (PA_STRUCT). Thus, I can write:
  101.  
  102.     PA_STRUCT    background;    /* P tells me it's a pointer type */
  103.  
  104.     background = (PA_STRUCT)calloc(100, sizeof(A_STRUCT));
  105.  
  106. HTH,
  107.  
  108.  AriL
  109.  
  110. All my opinions are mine and mine alone.
  111.  
  112.